﻿using System;
using System.Reflection;
using System.Collections.Generic;
using System.Text;

namespace ORM.Annotations
{
    public class ORMAnalyzer : ORM.Annotations.IORMAnalyzer
    {
        private EntityTable tableInfo;
        private IList<EntityFeild> feildInfoes;
        private EntityFeild primaryKey;
        private string sqlFeildsString;

        public string SqlFeildsString
        {
            get
            {
                if (String.IsNullOrEmpty(sqlFeildsString))
                {
                    StringBuilder builder = new StringBuilder();
                    if (null != this.primaryKey)
                    {
                        builder.Append(this.primaryKey.Name);
                    }
                    foreach (EntityFeild entry in feildInfoes)
                    {
                        if (null != this.primaryKey && this.primaryKey.Equals(entry))
                        {
                            continue;
                        }
                        if (builder.Length == 0)
                        {
                            builder.Append(entry.Name);
                        }
                        else
                        {
                            builder.Append(",").Append(entry.Name);
                        }
                    }
                    this.sqlFeildsString = builder.ToString();
                }
                return sqlFeildsString;
            }
        }

        public EntityTable TableInfo
        {
            get { return tableInfo; }
        }

        public IList<EntityFeild> FeildInfoes
        {
            get { return feildInfoes; }
        }

        public EntityFeild PrimaryKey
        {
            get { return primaryKey; }
        }

        public ORMAnalyzer(Type model)
        {
            if (model.IsDefined(typeof(EntityTable), false))
            {
                object[] attributes = model.GetCustomAttributes(typeof(EntityTable), false);
                this.tableInfo = (EntityTable)attributes[0];
                this.tableInfo.Name = this.tableInfo.Name.ToUpper();
            }
            else
            {
                throw new ORMAnalyzerException("Is not EntityTable :" + model.FullName);
            }

            PropertyInfo[] properties = model.GetProperties();
            this.feildInfoes = new List<EntityFeild>();
            foreach (PropertyInfo entry in properties)
            {
                if (entry.IsDefined(typeof(EntityFeild), false))
                {
                    EntityFeild feild = (EntityFeild)entry.GetCustomAttributes(typeof(EntityFeild), false)[0];
                    feild.FeildRefInfo = entry;
                    this.feildInfoes.Add(feild);
                    if (feild.Id)
                    {
                        this.primaryKey = feild;
                    }
                }
            }
        }
    }
}
